package br.com.data.hibernate;

import java.sql.SQLException;
import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import br.com.arquitetura.data.GenericDao;
import br.com.model.entidade.Aluno;
import br.com.model.entidade.Aula;
import br.com.model.entidade.Escola;
import br.com.model.entidade.Progresso;

@Repository
public class ProgressoHibernateDAO extends GenericDao{

	@Autowired
	public ProgressoHibernateDAO(SessionFactory sessionFactory) {
		// TODO Auto-generated constructor stub
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public Progresso buscarProgresso(final Aluno aluno, final Aula aula) {

		return (Progresso) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					// O HibernateCallback possibilita o acesso direto API do
					// Hibernate para a execuo de consultas mais complexas que
					// no so cobertas pelo HibernateTemplate
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria c = session.createCriteria(Progresso.class);

						c.add(Restrictions.eq("aluno", aluno));
						c.add(Restrictions.eq("aula", aula));
						
						c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
						System.out.println(c.list().size());
						return c.uniqueResult();
					}

				});
	}
	
	
}
